package com.ruoyi.factorydata.controller;

import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.factorydata.domain.FactoryData;
import com.ruoyi.factorydata.domain.dto.FactoryDataDTO;
import com.ruoyi.factorydata.domain.vo.FactoryDataVo;
import com.ruoyi.factorydata.domain.vo.FactoryTreeVo;
import com.ruoyi.factorydata.service.IFactoryDataService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 工厂建模信息Controller
 * 
 * @author ruoyi
 * @date 2025-06-22
 */
@RestController
@RequestMapping("/factory/data")
public class FactoryDataController extends BaseController
{
    @Autowired
    private IFactoryDataService factoryDataService;

    /**
     * 查询工厂建模信息列表
     */
    @PreAuthorize("@ss.hasPermi('factory:data:list')")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody FactoryDataDTO factoryDataDTO)
    {
        startPage();
        List<FactoryDataVo> list = factoryDataService.selectFactoryDataList(factoryDataDTO);
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setRows(list);
        tableDataInfo.setTotal(new PageInfo(list).getTotal());
        return tableDataInfo;
    }

    @GetMapping("/selectFactoryByKeyword")
    public AjaxResult selectFactoryByKeyword(@RequestParam(required = false) String keyword) {
        startPage();
        List<FactoryData> list = factoryDataService.list(new QueryWrapper<FactoryData>().lambda()
                .like(keyword != null && !keyword.isBlank(), FactoryData::getFactoryName, keyword));
        return AjaxResult.success(list);
    }

    @GetMapping("/treeList")
    public AjaxResult treeList(FactoryData factoryData){
        List<FactoryTreeVo> factoryTreeVos = factoryDataService.selectFactoryTreeList(factoryData);
        return success(factoryTreeVos);
    }
    /**
     * 导出工厂建模信息列表
     */
    @PreAuthorize("@ss.hasPermi('factory:data:export')")
    @Log(title = "工厂建模信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FactoryDataDTO factoryDataDTO)
    {
        List<FactoryDataVo> list = factoryDataService.selectFactoryDataList(factoryDataDTO);
        ExcelUtil<FactoryDataVo> util = new ExcelUtil<FactoryDataVo>(FactoryDataVo.class);
        util.exportExcel(response, list, "工厂建模信息数据");
    }

    /**
     * 获取工厂建模信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('factory:data:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(factoryDataService.selectFactoryDataById(id));
    }

    /**
     * 新增工厂建模信息
     */
    @PreAuthorize("@ss.hasPermi('factory:data:add')")
    @Log(title = "工厂建模信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FactoryData factoryData)
    {
      boolean exists =  factoryDataService.lambdaQuery().eq(FactoryData::getFactoryCode, factoryData.getFactoryCode()).count() > 0;
      if (exists){
          return AjaxResult.warn("工厂编码已存在");
      }
        return toAjax(factoryDataService.insertFactoryData(factoryData));
    }

    /**
     * 修改工厂建模信息
     */
    @PreAuthorize("@ss.hasPermi('factory:data:edit')")
    @Log(title = "工厂建模信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FactoryData factoryData)
    {
        List<Long> ids = factoryDataService.lambdaQuery()
                .eq(FactoryData::getFactoryCode, factoryData.getFactoryCode())
                .select(FactoryData::getId).list().stream()
                .map(FactoryData::getId)
                .collect(Collectors.toList());
        boolean exists = !ids.isEmpty() && !ids.contains(factoryData.getId());
        if (exists) {
            return AjaxResult.error("工厂编码已存在");
        }
        return toAjax(factoryDataService.updateFactoryData(factoryData));
    }

    /**
     * 删除工厂建模信息
     */
    @PreAuthorize("@ss.hasPermi('factory:data:remove')")
    @Log(title = "工厂建模信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(factoryDataService.deleteFactoryDataByIds(ids));
    }
}
